package com.ry.novel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ry.novel.mapper.UserMapper;
import com.ry.novel.model.dto.AuthorInfo;
import com.ry.novel.model.dto.UserInfo;
import com.ry.novel.model.dto.condition.UserQuery;
import com.ry.novel.model.entity.User;
import com.ry.novel.service.AuthorService;
import com.ry.novel.service.ReaderService;
import com.ry.novel.utils.DateUtil;
import com.ry.novel.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.charset.Charset;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ry
 * @since 2022-04-03 16:05
 **/
@Service
public class ReaderServiceImpl implements ReaderService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AuthorService authorService;

    @Override
    public Page<UserInfo> queryReader(UserQuery userQuery) {
        Page<User> page = new Page<>(userQuery.getPage(),userQuery.getSize());
        Page<UserInfo> infoPage = new Page<>(userQuery.getPage(),userQuery.getSize());
        QueryWrapper<User> wrapper = new QueryWrapper<>();

        //添加查询条件
        if (userQuery.getUsername() != null){
            wrapper.like("username",userQuery.getUsername());
        }

        userMapper.selectPage(page,wrapper);

        List<UserInfo> infos = page.getRecords().stream().map(entity -> {
            UserInfo userInfo = getUserInfo(entity);

            return userInfo;
        }).collect(Collectors.toList());

        infoPage.setRecords(infos);
        infoPage.setTotal(page.getTotal());


        return infoPage;
    }

    @Override
    public void deleteReader(Long id) {
        userMapper.deleteById(id);
    }

    @Override
    public void addReader(UserInfo reader) {
        User user = new User();
        user.setUsername(reader.getUsername());
        String rawPw = reader.getPassword();
        String encodePass = MD5Util.MD5Encode(rawPw, Charset.forName("utf8").name());
        user.setPassword(encodePass);
        user.setNickname(reader.getUsername());
        user.setUserPhoto(reader.getUserPhoto());
        user.setGender(reader.getGender());
        user.setState(reader.getState());
        user.setCreateTime(LocalDateTime.now().toString());

        userMapper.insert(user);
    }

    @Override
    public void updateReader(UserInfo reader) {
        User user = new User();
        user.setId(reader.getId());
        String rawPw = reader.getPassword();
        if (rawPw != null){
            String encodePass = MD5Util.MD5Encode(rawPw, Charset.forName("utf8").name());
            user.setPassword(encodePass);
        }
        user.setUsername(reader.getUsername());
        user.setNickname(reader.getNickname());
        user.setUserPhoto(reader.getUserPhoto());
        user.setGender(reader.getGender());
        user.setState(reader.getState());

        userMapper.updateById(user);
    }

    /**
     * 查询出所有未成为作者的用户
     *
     * @return
     */
    @Override
    public List<UserInfo> queryNotAuthor() {
        QueryWrapper<User> wrapper = new QueryWrapper<>();

        //查询出所有的作者
        List<AuthorInfo> authorInfos = authorService.queryAll();
        List<Long> userIds = authorInfos.stream().map(info -> info.getUserId()).collect(Collectors.toList());
        //id不在userIds中
        wrapper.notIn("id",userIds);

        List<User> users = userMapper.selectList(wrapper);
        List<UserInfo> userInfos = users.stream().map(entity -> {
            UserInfo userInfo = getUserInfo(entity);
            return userInfo;
        }).collect(Collectors.toList());

        return userInfos;
    }

    @Override
    public UserInfo queryById(Long id) {
        User user = userMapper.selectById(id);
        UserInfo userInfo = getUserInfo(user);
        return userInfo;
    }

    @Override
    public List<UserInfo> queryAll() {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        List<User> users = userMapper.selectList(wrapper);
        List<UserInfo> infos = users.stream().map(entity -> {
            UserInfo userInfo = getUserInfo(entity);

            return userInfo;
        }).collect(Collectors.toList());

        return infos;
    }

    public UserInfo getUserInfo(User user){
        UserInfo userInfo = new UserInfo();
        userInfo.setId(user.getId());
        userInfo.setUsername(user.getUsername());
        userInfo.setNickname(user.getNickname());
        userInfo.setUserPhoto(user.getUserPhoto());
        userInfo.setGender(user.getGender());
        userInfo.setState(user.getState());
        userInfo.setCreateTime(DateUtil.format(user.getCreateTime()));

        return userInfo;
    }
}
